﻿//////////////////////////////////////////////////
//               SettingForm.cs                 //
//         Part of RebornCo by kaihaider        //
//////////////////////////////////////////////////
using System;
using System.Linq;
using System.Windows.Forms;
using RebornCo.Helpers;
using RebornCo.Rotations;

namespace RebornCo.Settings
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = RebornCoRoutine.WindowSettings;
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            RebornCoRoutine.WindowSettings.Save();

            RebornCoRoutine.WindowSettings.TankId = 0;

            if (RebornCoRoutine.WindowSettings.LastHotKeyChoice != RebornCoRoutine.WindowSettings.HotKeyChoice ||
                RebornCoRoutine.WindowSettings.LastModKeyChoice != RebornCoRoutine.WindowSettings.ModKeyChoice)
            {
                ff14bot.Managers.HotkeyManager.Unregister("RotationSwap");
                ff14bot.Managers.HotkeyManager.Register("RotationSwap", (Keys) System.Windows.Input.KeyInterop.VirtualKeyFromKey(RebornCoRoutine.WindowSettings.HotKeyChoice), 
                    (System.Windows.Input.ModifierKeys) RebornCoRoutine.WindowSettings.ModKeyChoice, 
                    hk => 
                    { 
                        Helpers.Spell.RotationSwap = !Helpers.Spell.RotationSwap; 
                        ff14bot.Helpers.Logging.Write(Helpers.Spell.RotationSwap ? "[RebornCo] Secondary Rotation Activated!" : "[RebornCo] Primary Rotation Resumed!"); 
                    });
                RebornCoRoutine.WindowSettings.LastHotKeyChoice = RebornCoRoutine.WindowSettings.HotKeyChoice;
                RebornCoRoutine.WindowSettings.LastModKeyChoice = RebornCoRoutine.WindowSettings.ModKeyChoice;
            }
            if (RebornCoRoutine.WindowSettings as WhiteMageSetting != null)
            {
                if ((RebornCoRoutine.WindowSettings as WhiteMageSetting).MedicaOverhealCount >= (RebornCoRoutine.WindowSettings as WhiteMageSetting).MedicaCount)
                    (RebornCoRoutine.WindowSettings as WhiteMageSetting).MedicaOverhealCount =
                        (RebornCoRoutine.WindowSettings as WhiteMageSetting).MedicaCount - 1;
                (RebornCoRoutine.WindowSettings as WhiteMageSetting).EsunaSpells = (RebornCoRoutine.WindowSettings as WhiteMageSetting).EsunaSpells.Replace(" ", string.Empty).ToLower();
                (RebornCoRoutine.WindowSettings as WhiteMageSetting).EsunaList = (RebornCoRoutine.WindowSettings as WhiteMageSetting).EsunaSpells.Split(',').ToList();
            }
            if (RebornCoRoutine.WindowSettings as MarauderSetting != null
                && (((MarauderSetting)RebornCoRoutine.WindowSettings).LastStormsPathHotKeyChoice != ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsPathHotKeyChoice
                || ((MarauderSetting)RebornCoRoutine.WindowSettings).LastStormsPathModKeyChoice != ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsPathModKeyChoice))
            {
                ff14bot.Managers.HotkeyManager.Unregister("StormsPath");
                ff14bot.Managers.HotkeyManager.Register("StormsPath",
                    (Keys) System.Windows.Input.KeyInterop.VirtualKeyFromKey(((MarauderSetting) RebornCoRoutine.WindowSettings).StormsPathHotKeyChoice),
                    (System.Windows.Input.ModifierKeys)
                        ((MarauderSetting) RebornCoRoutine.WindowSettings).StormsPathModKeyChoice,
                    hk =>
                    {
                        Marauder.StormsPath =
                            !Marauder.StormsPath;
                        ff14bot.Helpers.Logging.Write(Marauder.StormsPath
                            ? "[RebornCo] Storm's Path will be used!"
                            : "[RebornCo] Storm's Path combo halted!");
                    });
                 ((MarauderSetting) RebornCoRoutine.WindowSettings).LastStormsPathHotKeyChoice = ((MarauderSetting) RebornCoRoutine.WindowSettings).StormsPathHotKeyChoice;
                ((MarauderSetting) RebornCoRoutine.WindowSettings).LastStormsPathModKeyChoice = ((MarauderSetting) RebornCoRoutine.WindowSettings).StormsPathModKeyChoice;
            }
            if (RebornCoRoutine.WindowSettings as MarauderSetting != null
    && (((MarauderSetting)RebornCoRoutine.WindowSettings).LastStormsEyeHotKeyChoice != ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsEyeHotKeyChoice
    || ((MarauderSetting)RebornCoRoutine.WindowSettings).LastStormsEyeModKeyChoice != ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsEyeModKeyChoice))
            {
                ff14bot.Managers.HotkeyManager.Unregister("StormsEye");
                ff14bot.Managers.HotkeyManager.Register("StormsEye",
                    (Keys)System.Windows.Input.KeyInterop.VirtualKeyFromKey(((MarauderSetting)RebornCoRoutine.WindowSettings).StormsEyeHotKeyChoice),
                    (System.Windows.Input.ModifierKeys)
                        ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsEyeModKeyChoice,
                    hk =>
                    {
                        Marauder.StormsEye =
                            !Marauder.StormsEye;
                        ff14bot.Helpers.Logging.Write(Marauder.StormsEye
                            ? "[RebornCo] Storm's Eye will be used!"
                            : "[RebornCo] Storm's Eye combo halted!");
                    });
                ((MarauderSetting)RebornCoRoutine.WindowSettings).LastStormsEyeHotKeyChoice = ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsEyeHotKeyChoice;
                ((MarauderSetting)RebornCoRoutine.WindowSettings).LastStormsEyeModKeyChoice = ((MarauderSetting)RebornCoRoutine.WindowSettings).StormsEyeModKeyChoice;
            }

            if (RebornCoRoutine.WindowSettings as RogueSetting != null)
            {
                (RebornCoRoutine.WindowSettings as RogueSetting).JugulateSpells = (RebornCoRoutine.WindowSettings as RogueSetting).JugulateSpells.Replace(" ", string.Empty).ToLower();
                (RebornCoRoutine.WindowSettings as RogueSetting).JugulateList = (RebornCoRoutine.WindowSettings as RogueSetting).JugulateSpells.Split(',').ToList();
                (RebornCoRoutine.WindowSettings as RogueSetting).ShurikenMobs = (RebornCoRoutine.WindowSettings as RogueSetting).ShurikenMobs.Replace(" ", string.Empty).ToLower();
                (RebornCoRoutine.WindowSettings as RogueSetting).ShurikenList = (RebornCoRoutine.WindowSettings as RogueSetting).ShurikenMobs.Split(',').ToList();
            }
            if (RebornCoRoutine.WindowSettings as RogueSetting != null
                && (((RogueSetting)RebornCoRoutine.WindowSettings).LastJugulateHotKeyChoice != ((RogueSetting)RebornCoRoutine.WindowSettings).JugulateHotKeyChoice
                || ((RogueSetting)RebornCoRoutine.WindowSettings).LastJugulateModKeyChoice != ((RogueSetting)RebornCoRoutine.WindowSettings).JugulateModKeyChoice))
            {
                ff14bot.Managers.HotkeyManager.Unregister("Jugulate");
                ff14bot.Managers.HotkeyManager.Register("Jugulate",
                    (Keys)System.Windows.Input.KeyInterop.VirtualKeyFromKey(((RogueSetting)RebornCoRoutine.WindowSettings).JugulateHotKeyChoice),
                    (System.Windows.Input.ModifierKeys)
                        ((RogueSetting)RebornCoRoutine.WindowSettings).JugulateModKeyChoice,
                    hk =>
                    {
                        ((RogueSetting)RebornCoRoutine.WindowSettings).JugulateDps = !((RogueSetting)RebornCoRoutine.WindowSettings).JugulateDps;
                        ff14bot.Helpers.Logging.Write(((RogueSetting)RebornCoRoutine.WindowSettings).JugulateDps
                        ? "[RebornCo] Jugulate will be used for dps!"
                        : "[RebornCo] Jugulate halted!");
                    });
                ((RogueSetting)RebornCoRoutine.WindowSettings).LastJugulateHotKeyChoice = ((RogueSetting)RebornCoRoutine.WindowSettings).JugulateHotKeyChoice;
                ((RogueSetting)RebornCoRoutine.WindowSettings).LastJugulateModKeyChoice = ((RogueSetting)RebornCoRoutine.WindowSettings).JugulateModKeyChoice;
            }

            if (RebornCoRoutine.WindowSettings as RogueSetting != null
                && (((RogueSetting)RebornCoRoutine.WindowSettings).LastDancingEdgeHotKeyChoice != ((RogueSetting)RebornCoRoutine.WindowSettings).DancingEdgeHotKeyChoice
                || ((RogueSetting)RebornCoRoutine.WindowSettings).LastDancingEdgeModKeyChoice != ((RogueSetting)RebornCoRoutine.WindowSettings).DancingEdgeModKeyChoice))
            {
                ff14bot.Managers.HotkeyManager.Unregister("DancingEdge");
                ff14bot.Managers.HotkeyManager.Register("DancingEdge",
                    (Keys)System.Windows.Input.KeyInterop.VirtualKeyFromKey(((RogueSetting)RebornCoRoutine.WindowSettings).DancingEdgeHotKeyChoice),
                    (System.Windows.Input.ModifierKeys)
                        ((RogueSetting)RebornCoRoutine.WindowSettings).DancingEdgeModKeyChoice,
                    hk =>
                    {
                        Ninja.DancingEdge = !Ninja.DancingEdge;
                        ff14bot.Helpers.Logging.Write(Ninja.DancingEdge
                        ? "[RebornCo] Dancing Edge will be used!"
                        : "[RebornCo] Dancing Edge combo halted!");
                    });
                ((RogueSetting)RebornCoRoutine.WindowSettings).LastDancingEdgeHotKeyChoice = ((RogueSetting)RebornCoRoutine.WindowSettings).DancingEdgeHotKeyChoice;
                ((RogueSetting)RebornCoRoutine.WindowSettings).LastDancingEdgeModKeyChoice = ((RogueSetting)RebornCoRoutine.WindowSettings).DancingEdgeModKeyChoice;
            }

            if (RebornCoRoutine.WindowSettings as RogueSetting != null
    && (((RogueSetting)RebornCoRoutine.WindowSettings).LastDotsHotKeyChoice != ((RogueSetting)RebornCoRoutine.WindowSettings).DotsHotKeyChoice
    || ((RogueSetting)RebornCoRoutine.WindowSettings).LastDotsModKeyChoice != ((RogueSetting)RebornCoRoutine.WindowSettings).DotsModKeyChoice))
            {
                ff14bot.Managers.HotkeyManager.Unregister("Dots");
                ff14bot.Managers.HotkeyManager.Register("Dots",
                    (Keys)System.Windows.Input.KeyInterop.VirtualKeyFromKey(((RogueSetting)RebornCoRoutine.WindowSettings).DotsHotKeyChoice),
                    (System.Windows.Input.ModifierKeys)
                        ((RogueSetting)RebornCoRoutine.WindowSettings).DotsModKeyChoice,
                    hk =>
                    {
                        Ninja.Dots = !Ninja.Dots;
                        ff14bot.Helpers.Logging.Write(Ninja.Dots
                            ? "[RebornCo] Dots will be used!"
                            : "[RebornCo] Dots halted!");
                    });
                ((RogueSetting)RebornCoRoutine.WindowSettings).LastDotsHotKeyChoice = ((RogueSetting)RebornCoRoutine.WindowSettings).DotsHotKeyChoice;
                ((RogueSetting)RebornCoRoutine.WindowSettings).LastDotsModKeyChoice = ((RogueSetting)RebornCoRoutine.WindowSettings).DotsModKeyChoice;
            }
            var pot = ff14bot.Managers.InventoryManager.FilledSlots.FirstOrDefault(
                a => a.Name.Equals(RebornCoRoutine.WindowSettings.PotionName));
            if(pot!=null && pot.IsValid)
            {
                Spell.Potion = pot;
                ff14bot.Helpers.Logging.Write("[RebornCo] Found: " + pot.Name);
            }
            RebornCoRoutine.Instance.UpdateContext();
        }
    }
}